package cn.itcast.star.graph.core.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.itcast.star.graph.core.service.OllamaService;
import cn.itcast.star.graph.ollama.client.api.OllamaApi;
import cn.itcast.star.graph.ollama.client.pojo.OllamaChatRequest;
import cn.itcast.star.graph.ollama.client.pojo.OllamaChatRespone;
import cn.itcast.star.graph.ollama.client.pojo.OllamaMessage;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import retrofit2.Call;
import retrofit2.Response;

import java.io.IOException;
import java.util.List;

/**
 * @author Windows
 */
@Service
public class OllamaServiceImpl implements OllamaService {

    @Resource
    private OllamaApi ollamaApi;

    /**
     * 调用千问大模型，将中文翻译成英文
     * @param text
     * @return
     */
    @Override
    public String translate(String text) {
        OllamaChatRequest request = new OllamaChatRequest();
        OllamaMessage message = new OllamaMessage();
        //封装请求参数
        request.setModel("qwen2:0.5b");
        message.setRole("user");
        message.setContent("请帮我把下面的内容翻译成英文：" + text);
        request.setMessages(List.of(message));
        //调用ollamaApi接口的chat方法
        Call<OllamaChatRespone> chat = ollamaApi.chat(request);
        //返回结果
        Response<OllamaChatRespone> execute;
        String content = null;
        try {
            execute = chat.execute();
            if (execute.isSuccessful()) {
                OllamaChatRespone body = execute.body();
                if (body != null) {
                    content = body.getMessage().getContent();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return content;
    }
}
